package org.jeecg.modules.meter.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.meter.Meter;
import org.jeecg.entity.meter.MeterControlSwitch;
import org.jeecg.entity.meter.MeterGateway;
import org.jeecg.enums.meter.MeterCallBackTypeEnum;
import org.jeecg.modules.meter.mapper.MeterControlSwitchMapper;
import org.jeecg.modules.meter.mapper.MeterMapper;
import org.jeecg.modules.meter.service.IMeterControlSwitchService;
import org.jeecg.modules.meter.service.IMeterGatewayService;
import org.jeecg.modules.meter.service.MeterIotService;
import org.jeecg.vo.meter.MeterSwitchRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 仪表通断记录表
 * @Author: jeecg-boot
 * @Date:   2020-11-20
 * @Version: V1.0
 */
@Service
@Slf4j
public class MeterControlSwitchServiceImpl extends ServiceImpl<MeterControlSwitchMapper, MeterControlSwitch> implements IMeterControlSwitchService {
    @Autowired
    private MeterControlSwitchMapper meterControlSwitchMapper;
    @Autowired
    private MeterMapper meterMapper;
    @Autowired
    private MeterIotService meterIotService;

    @Autowired
    private IMeterGatewayService meterGatewayService;


    @Override
    public boolean controlSwitchRequest(String meterId,Integer state) {
        MeterSwitchRespVo respVo = new MeterSwitchRespVo();
        respVo.setMeterId(meterId);
        respVo.setState(state);
        Meter meter = meterMapper.selectById(meterId);
        respVo.setTenantId(meter.getTenantId());
        return executeControlSwitchMeter(respVo);
    }

    @Override
    public void controlSwitchMeter(List<MeterSwitchRespVo> meterSwitchRespVoList) {
        for (MeterSwitchRespVo respVo:meterSwitchRespVoList){
            executeControlSwitchMeter(respVo);
        }
    }
    @Override
    public int updateCallbackStatus(MeterControlSwitch meterControl) {
        return meterControlSwitchMapper.updateCallbackStatus(meterControl);
    }

    @Override
    public List<MeterSwitchRespVo> getSwitchRecord(String meterId) {
        return meterControlSwitchMapper.getSwitchRecord(meterId);
    }

    @Override
    public MeterControlSwitch selectById(String requestCode) {
        return meterControlSwitchMapper.queryById(requestCode);
    }


    private boolean executeControlSwitchMeter(MeterSwitchRespVo respVo){
        MeterControlSwitch controlSwitch = new MeterControlSwitch();
        controlSwitch.setId(UUID.randomUUID().toString().replace("-",""));
        controlSwitch.setCommand(respVo.getState().toString());
        controlSwitch.setControlType(MeterCallBackTypeEnum.METER_CALL_BACK_TYPE_2.getCode()); //2：通断状态
        controlSwitch.setMeterId(respVo.getMeterId());
        controlSwitch.setTenantId(respVo.getTenantId());
        controlSwitch.setIsCallback(0);
        controlSwitch.setRequestTime(new Date());
        MeterGateway meterGateway = meterGatewayService.getByMeterId(respVo.getMeterId());
        if (meterGateway == null){
            log.warn("通断控制失败",meterGateway.getMeterCode() + "未配置网关！");
            return false;
        }
        if (StringUtils.isBlank(meterGateway.getMeterKey())){
            log.error("通断控制失败,表Key系统中不存在！");
            return false;
        }
        controlSwitch.setMeterGateway(meterGateway.getId());
        try {
            boolean response =  meterIotService.controlSwitchRequest(meterGateway.getMeterKey(),controlSwitch.getId(),respVo.getState(),null,meterGateway.getRequestUrl(),meterGateway.getIotType());
            if (!response){
                return false;
            }else {
                JSONObject requestJson = new JSONObject();
                requestJson.put("meterKey" ,meterGateway.getMeterKey());
                requestJson.put("requestCode" , controlSwitch.getId());
                requestJson.put("state" , respVo.getState());
                requestJson.put("requestUrl" , meterGateway.getRequestUrl());
                requestJson.put("iotType" , meterGateway.getIotType());
                controlSwitch.setRequest(requestJson.toJSONString());
                meterControlSwitchMapper.insert(controlSwitch);
            }

        } catch (Exception e){
            log.error("通断控制异常",e);
            return false;
        }
        return  true;
    }
}
